/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultsDisplay.java
 *
 * Created on 10-Feb-2009, 16:58:25
 */

package bluetoothserver.graphical;


import bluetoothserver.dataStore.Answer;
import bluetoothserver.dataStore.AnswerCollection;
import bluetoothserver.dataStore.AnswerUnits;
import bluetoothserver.dataStore.Question;
import bluetoothserver.dataStore.QuestionCollection;
import java.awt.Dimension;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Philip
 */
public class ResultsDisplay extends javax.swing.JFrame {
    private Question current;
    private ArrayList<AnswerUnits> words;
    /** Creates new form ResultsDisplay */
    public ResultsDisplay(Question q,String[] r) {
        current = q;
        if(current.getType() == QuestionCollection.SEQUENCE){
            current.setAnswers(r);
        }
        this.setUndecorated(true);
        initComponents();
    }
    
    public void run(){
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setSize(screenSize);
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        GraphicsDevice[] devices = env.getScreenDevices();
        for(int i = 0; i < devices.length;i++){
            boolean state = devices[i].isFullScreenSupported();
            
            this.setResizable(state);
            if(state){
                devices[i].setFullScreenWindow(this);
                validate();
            }
        }
        if((current.getType() == QuestionCollection.SEQUENCE)||(current.getType() == QuestionCollection.RATING)||(current.getType() == QuestionCollection.TEXT_ENTRY)){
            this.collateData();
        }else{
            this.standardCollate();
        }

        
        this.setVisible(true);
        if(current.getType() != 8){
            standardAnswers();
        }else{
            textDisplay();
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowStateListener(new java.awt.event.WindowStateListener() {
            public void windowStateChanged(java.awt.event.WindowEvent evt) {
                formWindowStateChanged(evt);
            }
        });

        jButton1.setText("Close");
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jButton1MousePressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton1))
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 560, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 356, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
        this.dispose();
    }//GEN-LAST:event_jButton1MousePressed

    private void formWindowStateChanged(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowStateChanged
        
    }//GEN-LAST:event_formWindowStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables

    private void standardCollate(){
        words = new ArrayList<AnswerUnits>();
        AnswerCollection ans = AnswerCollection.getInstance();
        
        String[] an = current.getAnswers();

        for(int i = 0;i < an.length;i++){
        Iterator<Answer> it = ans.getIterator();
            
            if((an[i].compareTo("")!=0)&&(an[i].compareTo("null")!=0)){
                System.out.println(an.length);
                int tot = 0;
                while(it.hasNext()){
                    Answer a = it.next();
                    if(a.getAnswer().compareToIgnoreCase(an[i])==0){
                        tot++;
                    }
                }
                AnswerUnits q = new AnswerUnits(an[i],tot);
                words.add(q);
                
            }
        }
    }

    private void collateData(){
        AnswerCollection ans = AnswerCollection.getInstance();
        
        Iterator<Answer> i = ans.getIterator();
        words = new ArrayList<AnswerUnits>();
        while(i.hasNext()){
            Answer a = i.next();
            String s = a.getAnswer();
            s = s.toLowerCase();
            if(!searchForWord(s)){
                Iterator<Answer> j = ans.getIterator();
                int k = 0;
                while(j.hasNext()){
                    Answer b = j.next();
                    String d = b.getAnswer();
                    d = d.toLowerCase();
                    if(s.compareTo(d)==0){
                        k++;
                    }
                }
                AnswerUnits q = new AnswerUnits(s,k);
                words.add(q);
            }
        }

    }
    
    private boolean searchForWord(String w){
        Iterator<AnswerUnits> i = words.iterator();
        while(i.hasNext()){
            AnswerUnits a = i.next();
            if(a.getAnswerName().compareTo(w)==0){
                return true;
            }
        }
        return false;
    }
    
    
    private void standardAnswers(){
        BarChart bar = new BarChart(current,words);
        PieChart pie = new PieChart(current,words);
        jTabbedPane1.addTab("Bar Chart", bar.getChart(this.getSize()));
        jTabbedPane1.addTab("Pie Chart", pie.getPieChart(this.getSize()));
    }

    private void textDisplay(){
        TextDisplay display = new TextDisplay(this.getWidth(),this.getHeight(),words);
        jTabbedPane1.addTab("Text Display", display);
        BarChart bar = new BarChart(current,words);
        PieChart pie = new PieChart(current,words);
        jTabbedPane1.addTab("Bar Chart", bar.getChart(this.getSize()));
        jTabbedPane1.addTab("Pie Chart", pie.getPieChart(this.getSize()));
    }
}
